﻿@{
    Layout = null;
}

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="/lib/layui-v2.6.3/css/layui.css" media="all">
    <link rel="stylesheet" href="/css/public.css" media="all">
    <style>
        body {
            background-color: #ffffff;
        }
    </style>
</head>
<body>

    <div id="ID-transfer-demo"></div>

    <script src="/lib/layui-v2.6.3/layui.js" charset="utf-8"></script>
    <script>
        layui.use(['form', 'transfer'], function () {
            var form = layui.form,
                layer = layui.layer,
                $ = layui.$;
            var transfer = layui.transfer;

            //角色id
            var roleId = window.location.search.substring(4);

            //发起异步请求获取穿梭框的备选数据，和已绑定的数据
            $.ajax({
                url: '/RoleInfo/GetBindMenuInfo',
                type: 'get',
                data: {
                    roleId: roleId
                },
                success: function (res) {
                    // 渲染
                    transfer.render({
                        elem: '#ID-transfer-demo',
                        data: res.data.options,
                        value: res.data.bindIds,
                        onchange: function (data, index) {
                            console.log(data); // 得到当前被穿梭的数据，数组
                            console.log(index); // 左边到右边0，右边到左边是1

                            var menuIds = [];
                            //处理data数组中的id，得到一个新的用户id数组
                            for (var i = 0; i < data.length; i++) {
                                //添加用户id到数组中
                                menuIds.push(data[i].value);
                            }


                            if (index == 0) { //绑定
                                $.ajax({
                                    url: '/RoleInfo/BindMenuInfo',
                                    type: 'post',
                                    data: {
                                        roleId: roleId,
                                        menuIds: menuIds
                                    },
                                    success: function (res) {
                                        layer.msg(res.msg);
                                    }
                                });

                            } else if (index == 1) { //解绑
                                $.ajax({
                                    url: '/RoleInfo/UnbindMenuInfo',
                                    type: 'post',
                                    data: {
                                        roleId: roleId,
                                        menuIds: menuIds
                                    },
                                    success: function (res) {
                                        layer.msg(res.msg);
                                    }
                                });
                            }
                        }
                    });

                }
            });

            // // 数据
            // var data = [
            //     { "value": "1", "title": "李白" },
            //     { "value": "2", "title": "杜甫" },
            //     { "value": "3", "title": "苏轼" },
            //     { "value": "4", "title": "李清照" },
            //     { "value": "5", "title": "鲁迅", "disabled": true },
            //     { "value": "6", "title": "巴金" },
            //     { "value": "7", "title": "冰心" },
            //     { "value": "8", "title": "矛盾" },
            //     { "value": "9", "title": "贤心" }
            // ];



        });
    </script>
</body>
</html>